package com.chenshu.aop;

import com.chenshu.aop.proxy.*;
import net.sf.cglib.proxy.Enhancer;

import java.lang.reflect.Proxy;

/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2024-04-21
 * Time：11:11
 */
public class Application {
    public static void main(String[] args) {
        //静态代理
//        Proxy proxy = new Proxy(new RealSubject());
//        proxy.takeAdvertise();
//        proxy.giveConcert();

        //JDK动态代理

        //目标类
        RealSubject target = new RealSubject();

        Subject proxy = (Subject) Proxy.newProxyInstance(
                target.getClass().getClassLoader(),
                new Class[] {Subject.class},
                new JDKInvocationHandler(target));

        proxy.giveConcert();
        proxy.takeAdvertise();

        //CGLIB动态代理
//        RealSubject2 target = new RealSubject2();
//        RealSubject2 proxy = (RealSubject2) Enhancer.create(
//                target.getClass(),
//                new CGLibMethodInterceptor(target));
//
//        proxy.takeAdvertise();
//        proxy.giveConcert();
    }
}
